<template>
  <div class="shopping-cart">
    <div class="shopping-cart-busket-header">
      <div style="float:left;margin-left:10px;">购物车</div>
      <div class="shopping-cart-clear-button" @click="clearShoppingCart">[清空]</div>
    </div>
    <div v-if="shoppingCart!== null" v-for="cartItem in shoppingCart" :key="cartItem.product.id" class="shopping-cart-busket">
      <div class="shopping-cart-busket-product-name">
        {{ cartItem.product.name }}
      </div>
      <div class="shopping-cart-busket-product-amount">
        {{ cartItem.amount }}份
      </div>
      <div class="shopping-cart-busket-product-cost">
        ￥{{ cartItem.product.price * cartItem.amount | filterCost }}
      </div>
      <div class="shopping-cart-busket-product-operation">
        <i class="el-icon-delete" @click="removeFromShoppingCart(cartItem.product)"></i>
      </div>
    </div>
    <div class="shopping-cart-total-cost">￥{{ shoppingCartTotalCost | filterCost }}</div>
    <div class="shopping-cart-submit-button" @click="gotoOrder">提交</div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import { ROUTENAME } from 'services/constConfig.js'

export default {
  data() {
    return {}
  },
  computed: mapGetters({
    shoppingCart: 'shoppingCart',
    shoppingCartTotalCost: 'shoppingCartTotalCost',
    nowStore: 'nowStore'
  }),
  filters: {
    filterCost(cost) {
      return cost.toFixed(1)
    }
  },
  methods: {
    ...mapActions({
      removeFromShoppingCart: 'removeFromShoppingCart',
      clearShoppingCart: 'clearShoppingCart'
    }),
    gotoOrder() {
      this.$router.push({ path: ROUTENAME.ORDER })
    }
  },
  mounted() {
    if (this.shoppingCart) {
      for (let item in this.shoppingCart) {
        if (this.shoppingCart[item].product.shopid !== this.nowStore.id) {
          this.clearShoppingCart()
        }
      }
    }
  }
}
</script>

<style scoped>
.shopping-cart {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 320px;
  height: auto;
  box-shadow: 0 1px 3px #2c2c2c;
}
.shopping-cart-busket-header {
  position: relative;
  margin-top: 0;
  height: 45px;
  width: 320px;
  line-height: 45px;
  text-align: center;
  background-color: #fafafa;
  font-size: 14px;
  border-top: 2px solid #0089dc;
  border-bottom: 1px solid #eee;
}
.shopping-cart-busket {
  position: relative;
  height: 45px;
  width: 320px;
  line-height: 45px;
  text-align: center;
  /* border-bottom: 1px solid #eee; */
  /* color: #ffffff; */
}
.shopping-cart-busket:hover {
  background-color: #f4f4f4;
}
.shopping-cart-busket-product-name {
  float: left;
  height: 45px;
  width: 150px;
  line-height: 45px;
  margin-left: 10px;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #666;
  text-align: left;
  border-bottom: 1px solid #eee;
}
.shopping-cart-busket-product-amount {
  float: left;
  height: 45px;
  width: 50px;
  line-height: 45px;
  font-size: 14px;
  color: #666;
  border-bottom: 1px solid #eee;
}
.shopping-cart-busket-product-cost {
  float: left;
  height: 45px;
  width: 75px;
  /* margin-right: 10px; */
  line-height: 45px;
  font-size: 14px;
  color: #f17530;
  border-bottom: 1px solid #eee;
}
.shopping-cart-busket-product-operation {
  float: left;
  height: 45px;
  width: 25px;
  margin-right: 10px;
  line-height: 45px;
  font-size: 14px;
  color: #666;
  border-bottom: 1px solid #eee;
}
.shopping-cart-busket-product-operation {
  cursor: pointer;
}
.shopping-cart-total-cost {
  position: relative;
  height: 45px;
  width: 200px;
  margin-bottom: 0;
  line-height: 45px;
  text-align: center;
  float: left;
  background-color: #2c2c2c;
  color: #ffffff;
  font-size: 24px;
}
.shopping-cart-submit-button {
  position: relative;
  height: 45px;
  width: 120px;
  margin-bottom: 0;
  line-height: 45px;
  text-align: center;
  float: right;
  background-color: #51d862;
  color: #ffffff;
}
.shopping-cart-submit-button:hover {
  cursor: pointer;
}
.shopping-cart-clear-button {
  float: right;
  margin-right: 10px;
  color: #0089dc;
}
.shopping-cart-clear-button:hover {
  cursor: pointer;
}
</style>

